One technique essential to high-dimensional data visualization is the ability to arrange multiple views.
By arranging multiple low-dimensional graphics of the same (or similar) high-dimensional data, one can put local summaries and patterns into a global context.
Ideally, when displaying multiple related data views, they are linked through an underlying data source to foster comparisons and enable posing of data queries.
The subplot() function provides a flexible interface for
merging multiple plotly objects into a single
object.
Its capabilities and interface are similar to the
grid.arrange() function from the gridExtra
package, which allows you to arrange multiple grid
grobs in a single view.
The most simple way to use subplot() is to directly
supply plotly objects:
library(plotly)
p1 <- plot_ly(economics, x = ~date, y = ~unemploy) %>%
add_lines(name = "unemploy")
p2 <- plot_ly(economics, x = ~date, y = ~uempmed) %>%
add_lines(name = "uempmed")
subplot(p1, p2)
Although subplot() accepts an arbitrary number of plot
objects, passing a list of plots can save typing and redundant
code when dealing with a large number of plots.
The following shows one time series for each variable in the
economics dataset and shares the x-axis so that zoom/pan
events are synchronized across each series:
vars <- setdiff(names(economics), "date")
plots <-
lapply(vars,
function(var) {
plot_ly(economics, x = ~date, y = as.formula(paste0("~", var))) %>%
add_lines(name = var)
}
)
subplot(plots, nrows = length(plots), shareX = TRUE, titleX = FALSE)
Conceptually, subplot() provides a way to place a
collection of plots into a table with a given number of rows and
columns.
By default, each row/column shares an equal proportion of the overall height/width.
The default can be changed via the heights and
widths arguments:
par(mar = c(0, 0, 0, 0))
par(mfrow = c(1, 1))
plot(imager::load.image("proportions.png"), axes = FALSE)
E.g., a joint density plot is really a subplot of joint and marginal densities.
Generate the following plot which shows a bivariate heatmap alongside marginal histograms (Hint: 2x2 grid):